import { ref } from 'vue'
import { LogIn } from '@vicons/ionicons5'
import { defineStore } from 'pinia'
import { IStore } from './index.d'
import { loginData, findData, addUserType } from './types'
import loginApi from '../../api/login'
import userApi from '../../api/user'
import { thingDark, useMessage } from 'naive-ui'
const message = useMessage()
export default defineStore('userStore', {
  state: (): IStore => ({
    testValue: 1,
    userInfo: {
      userName: '',
      avatar: ''
    }
  }),
  actions: {
    // 异步登录并获取用户信息
    async login(login_data: loginData) {
      const { code, data, msg } = await loginApi.login(login_data)
      if (code === 200) {
        this.getUserInfo()
      }
      return { code, data, msg }
    },
    //异步退出登录
    async logout() {
      const { code, data, msg } = await loginApi.logout()
      return { code, data, msg }
    },
    //获取登录用户信息
    async getUserInfo() {
      const { data } = await userApi.getInfo()
      this.userInfo.userName = data.userName
      this.userInfo.avatar = data.imagePath
      return { data }
    },
    //查询用户列表
    async getUserList(findData: findData) {
      const { data } = await userApi.getUserList(findData)
      return { data }
    },
    //删除用户
    async deleteUserById(id: number) {
      const { code } = await userApi.deleteUserById(id)
      return { code }
    },
    //添加用户
    async addUser(value: addUserType) {
      const { code, msg } = await userApi.addUser(value)
      return { code, msg }
    },
    // 重置用户信息
    resetUserInfo() {
      this.$reset()
    }
  },
  getters: {
    userInfoGetter: (state) => {
      return state.userInfo
    },
    testValueGetter(): number {
      return this.testValue
    }
  }
})
